public class Rule {
    static int armycreattime = PlayPanel.getTime();
    static int protecttime=0;//受保护时间
    static int leftoverBoss = StaticNumber.BOSS_NUMBER;//剩余Boss敌机数量
    static int beecreattime = PlayPanel.getTime();


    static void judge(){


        creatArmyplane();//创造敌机

        creatBee();//创造小蜜蜂

        eatBee();

        //小蜜蜂移动
        for (int i = 0; i < PlayPanel.getBees().size(); i++) {
            PlayPanel.getBees().get(i).Move();
        }


        heroBullet();//英雄机器创建子弹同时子弹越界判定


        bulletAndArmy();//英雄机子弹和敌机关系判定

        armyBulletAndHero();//敌机子弹和英雄机关系判定

        allArmyAndHero();//全部敌机（移动）和英雄的判定关系（）碰撞

        //如果英雄机阵亡，创建一个英雄机爆炸物，等待爆炸结束，游戏结束
        if (HeroPlane.getHeroplane()!=null && HeroPlane.getHeroplane().getBoold()<=0 && PlayPanel.getHeroboom() == null){
            PlayPanel.setHeroboom(new Boom(HeroPlane.getHeroplane()));
            PlayPanel.getBooms().add(PlayPanel.getHeroboom());
            HeroPlane.Destory();
        }

        //所有敌人打完了，游戏通关
        if (PlayPanel.getArmyplanes().size()<=0&&getLeftoverBoss()<=0&&PlayPanel.getTime()>StaticNumber.BOSS_LAND_TIME){
            PlayPanel.setGamemode(2);
            PlayPanel.clear();
        }
    }

    private static void eatBee() {
        //吃蜜蜂判定
        if (PlayPanel.getBees().size()>0){
            for (int i = 0; i < PlayPanel.getBees().size(); i++) {
                if (HeroPlane.getHeroplane()!=null && collide(HeroPlane.getHeroplane(),PlayPanel.getBees().get(i))){
                    //一级蜜蜂，attack+1
                    if (PlayPanel.getBees().get(i).getLevel()==1){
                        HeroPlane.getHeroplane().setAttack(HeroPlane.getHeroplane().getAttack()+1);
                        PlayPanel.getBees().remove(i);
                        i--;
                    }

                    //二级蜜蜂，boold+1
                    else if (PlayPanel.getBees().get(i).getLevel()==2){
                        HeroPlane.getHeroplane().setBoold(HeroPlane.getHeroplane().getBoold()+1);
                        PlayPanel.getBees().remove(i);
                        i--;

                    //三级蜜蜂，弹道数+1
                    }else if (PlayPanel.getBees().get(i).getLevel()==3){
                        if (HeroPlane.getHeroplane().getBulletnum()<3){
                            HeroPlane.getHeroplane().setBulletnum(HeroPlane.getHeroplane().getBulletnum()+1);
                            PlayPanel.getBees().remove(i);
                            i--;
                        }
                        else{
                            PlayPanel.setNowscore(PlayPanel.getNowscore()+100);
                            PlayPanel.getBees().remove(i);
                            i--;
                        }
                    }
                }
            }
        }
    }

    private static void allArmyAndHero() {
        //敌机和英雄机的关系（全部敌机）
        for (int i = 0; i < PlayPanel.getArmyplanes().size(); i++) {
            //Boss机发射子弹
            if (PlayPanel.getArmyplanes().get(i) instanceof ExtraPlane){
                ((ExtraPlane) PlayPanel.getArmyplanes().get(i)).creatBullet();
            }

            //移动
            PlayPanel.getArmyplanes().get(i).Move();

            //相撞，敌机和英雄机血量-1，惩罚。
            if (HeroPlane.getHeroplane()!=null && collide(HeroPlane.getHeroplane(),PlayPanel.getArmyplanes().get(i)))
            {
                //设置相撞间隔保护，英雄机和敌机被撞1s内都有伤害免疫
                if (protecttime<PlayPanel.getTime()){
                    //敌机和英雄机扣血
                    PlayPanel.getArmyplanes().get(i).setBoold(PlayPanel.getArmyplanes().get(i).getBoold()-1);
                    HeroPlane.getHeroplane().setBoold(HeroPlane.getHeroplane().getBoold()-1);

                    if (HeroPlane.getHeroplane().getBulletnum()>1){
                        HeroPlane.getHeroplane().setBulletnum(HeroPlane.getHeroplane().getBulletnum()-1);
                    }else if (HeroPlane.getHeroplane().getAttack()>1){
                        HeroPlane.getHeroplane().setAttack(HeroPlane.getHeroplane().getAttack()-1);
                    }else if (PlayPanel.getNowscore()> 20){
                        PlayPanel.setNowscore(PlayPanel.getNowscore()-20);
                    }else {
                        PlayPanel.setNowscore(0);
                    }

                    protecttime = PlayPanel.getTime()+StaticNumber.PROTECT;
                }

            }
            //敌机越界，摧毁敌机
            if (PlayPanel.getArmyplanes().get(i).getBoold()<=0||PlayPanel.getArmyplanes().get(i).getY()>900){
                PlayPanel.getBooms().add(new Boom(PlayPanel.getArmyplanes().get(i)));
                //打中加分
                PlayPanel.setNowscore(PlayPanel.getNowscore()+PlayPanel.getArmyplanes().get(i).getScore());
                PlayPanel.getArmyplanes().remove(i);
                i--;
            }

        }
    }

    private static void armyBulletAndHero() {

        //敌方子弹和英雄机的关系
        if (PlayPanel.getArmybullets().size()>0){
            for (int i = 0; i < PlayPanel.getArmybullets().size(); i++) {
                PlayPanel.getArmybullets().get(i).Move();
                //子弹越界，摧毁子弹
                if (PlayPanel.getArmybullets().get(i).getY()<0||PlayPanel.getArmybullets().get(i).getY()>StaticNumber.WINWID){
                    PlayPanel.getArmybullets().remove(i);
                    i--;
                }
                if (i<0) i = 0;
                //子弹击中英雄机，英雄机扣血
                if (PlayPanel.getArmybullets().size()>0 && HeroPlane.getHeroplane()!=null && collide(PlayPanel.getArmybullets().get(i), HeroPlane.getHeroplane())){

                    HeroPlane.getHeroplane().setBoold(HeroPlane.getHeroplane().getBoold()-PlayPanel.getArmybullets().get(i).getAttack());
                    PlayPanel.getArmybullets().remove(i);
                    i--;

                }
            }
        }
    }

    private static void bulletAndArmy() {
        //子弹和敌机的关系
        if (PlayPanel.getBullets().size()>0){
            for (int i = 0; i < PlayPanel.getBullets().size(); i++) {
                for (int j = 0; j < PlayPanel.getArmyplanes().size(); j++) {
                    if (collide(PlayPanel.getBullets().get(i),PlayPanel.getArmyplanes().get(j))&&PlayPanel.getArmyplanes().get(j).getBoold()>0){
                        if (HeroPlane.getHeroplane()!=null){
                            PlayPanel.getArmyplanes().get(j).setBoold(PlayPanel.getArmyplanes().get(j).getBoold()-PlayPanel.getBullets().get(i).getAttack());
                        }
                        PlayPanel.getBullets().remove(i);
                        i--;
                        if (i<0) i=0;
                        break;
                    }
                    //子弹越界，摧毁子弹
                    if (PlayPanel.getBullets().get(i).getY()>StaticNumber.WINHIG||PlayPanel.getBullets().get(i).getY()<0){
                        PlayPanel.getBullets().remove(i);
                        i--;

                    }
                }


            }
        }
    }


    private static void heroBullet() {
        //英雄机
        if (HeroPlane.getHeroplane()!=null){
            HeroPlane.getHeroplane().creatBullet();
        }

        //英雄机子弹
        if (PlayPanel.getBullets().size()>0){
            for (int i = 0; i < PlayPanel.getBullets().size(); i++) {
                PlayPanel.getBullets().get(i).Move();
                //子弹越界，摧毁子弹
                if (PlayPanel.getBullets().get(i).getY()<0||PlayPanel.getBullets().get(i).getY()>StaticNumber.WINWID){
                    PlayPanel.getBullets().remove(i);
                    i--;
                }

            }
        }
    }

    private static void creatBee() {
        //创建小蜜蜂
        if (beecreattime<PlayPanel.getTime()&&PlayPanel.getBees().size()<5){
            int i = (int)(Math.random()*100);
            if (i<50){
                PlayPanel.getBees().add(new Bee((int)((int)(Math.random()*StaticNumber.WINWID)),1));
            }else if (i< 30){
                PlayPanel.getBees().add(new Bee((int)((int)(Math.random()*StaticNumber.WINWID)),2));
            }else{
                PlayPanel.getBees().add(new Bee((int)((int)(Math.random()*StaticNumber.WINWID)),3));
            }
            beecreattime = PlayPanel.getTime()+(int)(Math.random()*StaticNumber.BEE_CREATE_MAXTIME)+StaticNumber.BEE_CREATE_MINTIME;
        }
    }

    private static void creatArmyplane() {
        // 创建敌机
        if (PlayPanel.getTime()<StaticNumber.BOSS_LAND_TIME){//30后Boss敌机登场，将不再产生其他敌机
            if (armycreattime<PlayPanel.getTime()&&PlayPanel.getGamemode()==1&&PlayPanel.getArmyplanes().size()<20){
                if ((int)(Math.random()*100)<StaticNumber.AIR_POSSIBILITY){
                    PlayPanel.getArmyplanes().add(new BigPlane((int)(Math.random()*StaticNumber.WINWID)));
                }else{
                    PlayPanel.getArmyplanes().add(new AirPlane((int)(Math.random()*StaticNumber.WINWID)));
                }
                armycreattime = PlayPanel.getTime()+(int)(Math.random()*StaticNumber.ARMY_CREAT_MAXTIME)+StaticNumber.ARMY_CREAT_MINTIME;
            }
        }else{
            if (leftoverBoss>0&&armycreattime<PlayPanel.getTime()&&PlayPanel.getGamemode()==1&&PlayPanel.getArmyplanes().size()<StaticNumber.ARMY_MAXNYMBER){
                PlayPanel.getArmyplanes().add(new ExtraPlane((int)(Math.random()*StaticNumber.WINWID)));
                armycreattime = PlayPanel.getTime()+(int)(Math.random()*StaticNumber.BOSS_CREAT_MAXTIME)+StaticNumber.BOSS_CREAT_MINTIME;
                setLeftoverBoss(getLeftoverBoss()-1);
            }
        }
    }


    //判断两个飞行物是否相撞,若两个矩形重叠，必然各自会有一个顶点在对方区域内
    static boolean collide(Flyer f1,Flyer f2){

        int r1x = f1.getX()+f1.getWidth()/2;
        int r1y = f1.getY()+f1.getHight()/2;
        int r2x = f2.getX()+f2.getWidth()/2;
        int r2y = f2.getY()+f2.getHight()/2;
        int r1 = f1.getHight()<f1.getWidth()?f1.getHight()/2:f1.getWidth()/2;
        int r2 = f2.getHight()<f2.getWidth()?f2.getHight()/2:f2.getWidth()/2;
        if (Math.sqrt(Math.pow((r1x-r2x),2)+Math.pow((r1y-r2y),2))<=r1+r2){
            return true;
        }else{
            return false;
        }
    }

//    static boolean recCollide(Flyer f1, Flyer f2){
//        if (
//                (f1.getX()>f2.getX()&&f1.getX()<f2.getX()+f2.getWidth()&&f1.getY()>f2.getY()&&f1.getY()<f2.getY()+f2.getHight())//左上点在对方范围内
//              ||(f1.getX()+f1.getWidth()>f2.getX()&&f1.getX()+f1.getWidth()<f2.getY()+f2.getWidth()&&f1.getY()>f2.getY()&&f1.getY()<f2.getY()+f2.getHight())//右上点在对方范围内
//              ||(f1.getX()>f2.getX()&&f1.getX()<f2.getX()+f2.getWidth()&&f1.getY()+f1.getHight()>f2.getY()&&f1.getY()+f1.getHight()<f2.getY()+f2.getHight())//左下点在对方范围内
//              ||(f1.getX()+f1.getWidth()>f2.getX()&&f1.getX()+f1.getWidth()<f2.getY()+f2.getWidth()&&f1.getY()+f1.getHight()>f2.getY()&&f1.getY()+f1.getHight()<f2.getY()+f2.getHight())//右下点在对方范围内
//        ){
//            return true;
//        }else{
//            return false;
//        }
//    }

    public static int getArmycreattime() {
        return armycreattime;
    }

    public static void setArmycreattime(int armycreattime) {
        Rule.armycreattime = armycreattime;
    }

    public static int getProtecttime() {
        return protecttime;
    }

    public static void setProtecttime(int protecttime) {
        Rule.protecttime = protecttime;
    }

    public static int getLeftoverBoss() {
        return leftoverBoss;
    }

    public static void setLeftoverBoss(int leftoverBoss) {
        Rule.leftoverBoss = leftoverBoss;
    }
}
